home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / ed3.zip / ED.C next >
Text File  |  1988-08-27  |  44KB  |  2,220 lines

  1. /*
  2.  *  ed - standard editor
  3.  *  ~~
  4.  *    Authors: Brian Beattie, Kees Bot, and others
  5.  *
  6.  * Copyright 1987 Brian Beattie Rights Reserved.
  7.  * Permission to copy or distribute granted under the following conditions:
  8.  * 1). No charge may be made other than reasonable charges for reproduction.
  9.  * 2). This notice must remain intact.
  10.  * 3). No further restrictions may be added.
  11.  *
  12.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13.  *  TurboC mods and cleanup 8/17/88 RAMontante.
  14.  *  Further information (posting headers, etc.) at end of file.
  15.  * _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  16.  */
  17.  
  18. int    version = 3;    /* used only in the "set" function, for i.d. */
  19.  
  20. #include <stdio.h>
  21.  
  22. #ifdef __TURBOC__
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <string.h>
  26. #include <io.h>
  27.  
  28. #define    _cleanup()    ;
  29.  
  30. #endif  /* ifdef __TURBOC__  */
  31.  
  32.  
  33. /*
  34.  *    #defines for non-printing ASCII characters
  35.  */
  36. #define NUL    0x00    /* ^@ */
  37. #define EOS    0x00    /* end of string */
  38. #define SOH    0x01    /* ^A */
  39. #define STX    0x02    /* ^B */
  40. #define ETX    0x03    /* ^C */
  41. #define EOT    0x04    /* ^D */
  42. #define ENQ    0x05    /* ^E */
  43. #define ACK    0x06    /* ^F */
  44. #define BEL    0x07    /* ^G */
  45. #define BS    0x08    /* ^H */
  46. #define HT    0x09    /* ^I */
  47. #define LF    0x0a    /* ^J */
  48. #define NL    '\n'
  49. #define VT    0x0b    /* ^K */
  50. #define FF    0x0c    /* ^L */
  51. #define CR    0x0d    /* ^M */
  52. #define SO    0x0e    /* ^N */
  53. #define SI    0x0f    /* ^O */
  54. #define DLE    0x10    /* ^P */
  55. #define DC1    0x11    /* ^Q */
  56. #define DC2    0x12    /* ^R */
  57. #define DC3    0x13    /* ^S */
  58. #define DC4    0x14    /* ^T */
  59. #define NAK    0x15    /* ^U */
  60. #define SYN    0x16    /* ^V */
  61. #define ETB    0x17    /* ^W */
  62. #define CAN    0x18    /* ^X */
  63. #define EM    0x19    /* ^Y */
  64. #define SUB    0x1a    /* ^Z */
  65. #define ESC    0x1b    /* ^[ */
  66. #define FS    0x1c    /* ^\ */
  67. #define GS    0x1d    /* ^] */
  68. #define RS    0x1e    /* ^^ */
  69. #define US    0x1f    /* ^_ */
  70. #define SP    0x20    /* space */
  71. #define DEL    0x7f    /* DEL*/
  72.  
  73.  
  74. /*    Definitions of meta-characters used in pattern matching
  75.  *    routines.  LITCHAR & NCCL are only used as token identifiers;
  76.  *    all the others are also both token identifier and actual symbol
  77.  *    used in the regular expression.
  78.  */
  79. #define BOL    '^'
  80. #define EOL    '$'
  81. #define ANY    '.'
  82. #define LITCHAR    'L'
  83. #define    ESCAPE    '\\'
  84. #define CCL    '['    /* Character class: [...] */
  85. #define CCLEND    ']'
  86. #define NEGATE    '~'
  87. #define NCCL    '!'    /* Negative character class [^...] */
  88. #define CLOSURE    '*'
  89. #define OR_SYM    '|'
  90. #define DITTO    '&'
  91. #define OPEN    '('
  92. #define CLOSE    ')'
  93.  
  94.  
  95. /* Largest permitted size for an expanded character class.  (i.e. the class
  96.  * [a-z] will expand into 26 symbols; [a-z0-9] will expand into 36.)
  97.  */
  98. #define CLS_SIZE    128
  99.  
  100.  
  101. #define TRUE    1
  102. #define FALSE    0
  103. #define ERR        -2
  104. #define FATAL        (ERR-1)
  105. #define CHANGED        (ERR-2)
  106. #define SET_FAIL    (ERR-3)
  107. #define SUB_FAIL    (ERR-4)
  108.  
  109.  
  110. #define    BUFFER_SIZE    2048    /* stream-buffer size:  == 1 hd cluster */
  111.  
  112. #define LINFREE    1    /* entry not in use */
  113. #define LGLOB    2       /* line marked global */
  114.  
  115. #define MAXLINE    512    /* max number of chars per line */
  116. #define MAXPAT    256    /* max number of chars per replacement pattern */
  117. #define MAXFNAME 256    /* max file name size */
  118.  
  119.  
  120. /**  Global variables  **/
  121.  
  122. /*  Tokens are used to hold pattern templates. (see makepat())  */
  123.  
  124. typedef    char    BITMAP;
  125. typedef struct token {
  126.     char        tok;
  127.     char        lchar;
  128.     BITMAP        *bitmap;
  129.     struct token    *next;
  130. } TOKEN;
  131.  
  132. #define TOKSIZE sizeof (TOKEN)
  133.  
  134.  
  135. struct    line {
  136.     int        l_stat;        /* empty, mark */
  137.     struct line    *l_prev;
  138.     struct line    *l_next;
  139.     char        l_buff[1];
  140. };
  141. typedef struct line    LINE;
  142.  
  143.  
  144. int    diag = 1;        /* diagnostic-output? flag */
  145. char    *paropen[9];
  146. char    *parclose[9];
  147. int    between, parnum;
  148. int    truncflg = 1;    /* truncate long line flag */
  149. int    eightbit = 1;    /* save eighth bit */
  150. int    nonascii;    /* count of non-ascii chars read */
  151. int    nullchar;    /* count of null chars read */
  152. int    truncated;    /* count of lines truncated */
  153. char    fname[MAXFNAME];
  154. int    fchanged;    /* file-changed? flag */
  155. int    nofname;
  156. int    mark['z'-'a'+1];
  157. TOKEN    *oldpat;
  158.  
  159. LINE    Line0;
  160. int    CurLn = 0;
  161. int    LastLn = 0;
  162. char    inlin[MAXLINE];
  163. int    pflag;
  164. int    Line1, Line2, nlines;
  165. int    nflg;        /* print line number flag */
  166. int    lflg;        /* print line in verbose mode */
  167. int    pflg;        /* print current line after each command */
  168. char    *inptr;        /* tty input buffer */
  169.  
  170. struct tbl {
  171.     char    *t_str;
  172.     int    *t_ptr;
  173.     int    t_val;
  174. } *t, tbl[] = {
  175.     "number",    &nflg,        TRUE,
  176.     "nonumber",    &nflg,        FALSE,
  177.     "list",        &lflg,        TRUE,
  178.     "nolist",    &lflg,        FALSE,
  179.     "eightbit",    &eightbit,    TRUE,
  180.     "noeightbit",    &eightbit,    FALSE,
  181.     0
  182. };
  183.  
  184.  
  185. /*-------------------------------------------------------------------------*/
  186.  
  187. #ifdef __TURBOC__        /*  prototypes (unneeded?)  */
  188.  
  189. void    prntln(char *, int, int);
  190. void    putcntl(char , FILE *);
  191. int    doprnt(int, int);
  192. LINE    *getptr(int);
  193. BITMAP    *makebitmap(unsigned);
  194. void    relink(LINE *, LINE *, LINE *, LINE *);
  195. int    del(int, int);
  196. int    ins(char *);
  197. char    *gettxt(int);
  198. int    append(int, int);
  199. char    *catsub(char *, char *, char *, char *, char *);
  200. char    *matchs(char *, TOKEN *, int);
  201. int    deflt(int, int);
  202. char    *maksub(char *sub, int subsz);
  203. TOKEN    *optpat(void);
  204.  
  205. #else    /* !__TURBOC__ */
  206.  
  207. extern    char    *strcpy();
  208. extern    int    *malloc();
  209. extern    LINE    *getptr();
  210. BITMAP    *makebitmap();
  211. extern    char    *gettxt();
  212. extern    char    *catsub();
  213. extern    char    *matchs();
  214. char    *maksub();
  215. TOKEN    *optpat();
  216.  
  217. #endif  /* __TURBOC__ */
  218.  
  219.  
  220. /*________  Macros  ________________________________________________________*/
  221.  
  222. #ifndef max
  223. #  define max(a,b)    ((a) > (b) ? (a) : (b))
  224. #endif
  225.  
  226. #ifndef min
  227. #  define min(a,b)    ((a) < (b) ? (a) : (b))
  228. #endif
  229.  
  230. #ifndef toupper
  231. #  define toupper(c)    ((c >= 'a' && c <= 'z') ? c-32 : c )
  232. #endif
  233.  
  234. /*  getpat -- Translate arg into a TOKEN string  */
  235. #define    getpat(arg)    makepat((arg), '\000')
  236.  
  237. #define nextln(l)    ((l)+1 > LastLn ? 0 : (l)+1)
  238. #define prevln(l)    ((l)-1 < 0 ? LastLn : (l)-1)
  239.  
  240. #define clrbuf()    del(1, LastLn)
  241.  
  242. #define    Skip_White_Space    {while (*inptr==SP || *inptr==HT) inptr++;}
  243.  
  244.  
  245. /*________  functions  ______________________________________________________*/
  246.  
  247. /*****************************************************************************
  248.  *    BITMAP.C -    makebitmap, setbit, testbit
  249.  *            bit-map manipulation routines.
  250.  *
  251.  *    Copyright (c) Allen I. Holub, all rights reserved.  This program may
  252.  *        for copied for personal, non-profit use only.
  253.  */
  254. BITMAP    *makebitmap( size )
  255. unsigned size;
  256. {
  257.     /*    Make a bit map with "size" bits.  The first entry in
  258.      *    the map is an "unsigned int" representing the maximum
  259.      *    bit.  The map itself is concatenated to this integer.
  260.      *    Return a pointer to a map on success, 0 if there's
  261.      *    not enough memory.
  262.      */
  263.  
  264.     unsigned *map, numbytes;
  265.  
  266.     numbytes = (size >> 3) + ((size & 0x07) ? 1 : 0 );
  267.  
  268. #ifdef DEBUG
  269.     printf("Making a %u bit map (%u bytes required)\n", size, numbytes);
  270. #endif
  271.  
  272.     if( map = (unsigned *) malloc( numbytes + sizeof(unsigned) ))
  273.         *map = size;
  274.  
  275.     return ((BITMAP *)map);
  276. }
  277.  
  278. setbit( c, map, val )
  279. unsigned    c, val;
  280. char        *map;
  281. {
  282.     /*    Set bit c in the map to val.
  283.      *    If c > map-size, 0 is returned, else 1 is returned.
  284.      */
  285.  
  286.     if( c >= *(unsigned *)map )    /* if c >= map size */
  287.         return 0;
  288.  
  289.     map += sizeof(unsigned);    /* skip past size */
  290.  
  291.     if( val )
  292.         map[c >> 3] |= 1 << (c & 0x07);
  293.     else
  294.         map[c >> 3] &= ~(1 << (c & 0x07));
  295.  
  296.     return( 1 );
  297. }
  298.  
  299. testbit( c, map )
  300. unsigned    c;
  301. char        *map;
  302. {
  303.     /*    Return 1 if the bit corresponding to c in map is set.
  304.      *    0 if it is not.
  305.      */
  306.  
  307.     if( c >= *(unsigned *)map )
  308.         return 0;
  309.  
  310.     map += sizeof(unsigned);
  311.  
  312.     return(map[ c >> 3 ] & (1 << (c & 0x07)));
  313. }
  314. /*********  end of BITMAP.C  ************************************************/
  315.  
  316.  
  317. /*    omatch.c
  318.  *
  319.  * Match one pattern element, pointed at by pat, with the character at
  320.  * **linp.  Return non-zero on match.  Otherwise, return 0.  *Linp is
  321.  * advanced to skip over the matched character; it is not advanced on
  322.  * failure.  The amount of advance is 0 for patterns that match null
  323.  * strings, 1 otherwise.  "boln" should point at the position that will
  324.  * match a BOL token.
  325.  */
  326. omatch(linp, pat, boln)
  327. char    **linp;
  328.